package com.team2.cyber_eye;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.team2.cyber_eye.ActivityHelpers.AdapterProduct;
import com.team2.cyber_eye.ActivityHelpers.PagerAdapter;
import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.shared.Product;
import com.team2.shared.Store;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.LinearGradient;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabContentFactory;

/**
 * The <code>TabsViewPagerFragmentActivity</code> class implements the Fragment activity that maintains a TabHost using a ViewPager.
 * @author mwho
 */
public class ProductTabsView extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

	private TabHost mTabHost;
	private ViewPager mViewPager;
	private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, ProductTabsView.TabInfo>();
	private PagerAdapter mPagerAdapter;
	private ArrayList<Product> onlineProducts = new ArrayList<Product>();
	private ArrayList<Product> localStoreProducts = new ArrayList<Product>();
	/**
	 * 
	 * @author mwho
	 * Maintains extrinsic info of a tab's construct 
	 */
	private class TabInfo {
		 private String tag;
         private Class<?> clss;
         private Bundle args;
         private Fragment fragment;
         TabInfo(String tag, Class<?> clazz, Bundle args) {
        	 this.tag = tag;
        	 this.clss = clazz;
        	 this.args = args;
         }
		
	}
	/**
	 * A simple factory that returns dummy views to the Tabhost
	 * @author mwho
	 */
	class TabFactory implements TabContentFactory {

		private final Context mContext;

	    /**
	     * @param context
	     */
	    public TabFactory(Context context) {
	        mContext = context;
	    }

	    /** (non-Javadoc)
	     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
	     */
	    public View createTabContent(String tag) {
	        View v = new View(mContext);
	        v.setMinimumWidth(0);
	        v.setMinimumHeight(0);
	        return v;
	    }

	}
	/** (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// Inflate the layout
		setContentView(R.layout.activity_product_list_tabs);
		
		Typeface font1 = Typeface.createFromAsset(getAssets(), "CanarsieSlab.ttf");
		
		TextView labelText = (TextView) findViewById(R.id.productListLabel);
		
		labelText.setTypeface(font1);
		
		Bundle extras = this.getIntent().getExtras();
		if (extras != null)
		{			
			try
			{
				onlineProducts = (ArrayList<Product>)(Object)extras.getParcelableArrayList("onlineProducts");
				
				localStoreProducts = (ArrayList<Product>)(Object)extras.getParcelableArrayList("localStoreProducts");
			}
			catch(Exception ex) {
				Util.LogError(ex); 
			}
		}
		
		ImageButton optionsBtn = (ImageButton) findViewById(R.id.optionsBtn);
		
		optionsBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            
	        	Intent intent = new Intent(ProductTabsView.this, Options.class);
	        	startActivity(intent);
	        	
	        }
	    });
		
		ImageButton homeBtn = (ImageButton) findViewById(R.id.homeButton);
		
		homeBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            
	        	Intent intent = new Intent(ProductTabsView.this, Home.class);
	        	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	startActivity(intent);
	        	
	        }
	    });
		
		// Initialise the TabHost
		this.initialiseTabHost(savedInstanceState);
		if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
		// Intialise ViewPager
		this.intialiseViewPager();
		
		ViewGroup vg1 = (ViewGroup) mTabHost.getTabWidget().getChildTabViewAt(0);
		TextView tv1 = (TextView) vg1.getChildAt(1);
		tv1.setTextSize(15);
		tv1.setText("ONLINE LIST");
		tv1.setTypeface(font1);
		tv1.setTextColor(getResources().getColor(R.color.black));
		tv1.setBackgroundResource(R.drawable.btn_background);
				
		ViewGroup vg2 = (ViewGroup) mTabHost.getTabWidget().getChildTabViewAt(1);
		TextView tv2 = (TextView) vg2.getChildAt(1);
		tv2.setTextSize(15);
		tv2.setText("LOCAL LIST");
		tv2.setTypeface(font1);
		tv2.setTextColor(getResources().getColor(R.color.black));
		tv2.setBackgroundResource(R.drawable.btn_background);

	}

	/** (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }
    
    /**
     * Initialise ViewPager
     */
    private void intialiseViewPager() {
		
		List<Fragment> fragments = new Vector<Fragment>();
		
		Bundle args1 = new Bundle();
		args1.putParcelableArrayList("products", onlineProducts);
		
		fragments.add(Fragment.instantiate(this, OnlineProductList.class.getName(), args1));	
		
		Bundle args2 = new Bundle();
		args2.putParcelableArrayList("products", localStoreProducts);
		
		fragments.add(Fragment.instantiate(this, LocalProductList.class.getName(), args2));
		
		this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
		//
		this.mViewPager = (ViewPager)super.findViewById(R.id.tabviewpager);
		this.mViewPager.setAdapter(this.mPagerAdapter);
		this.mViewPager.setOnPageChangeListener(this);
    }
	
	/**
	 * Initialise the Tab Host
	 */
	private void initialiseTabHost(Bundle args) {
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
       
        ProductTabsView.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("ONILNE_LIST").setIndicator(""), ( tabInfo = new TabInfo("ONILNE_LIST", OnlineProductList.class, args)));
        
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        ProductTabsView.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("LOCAL_LIST").setIndicator(""), ( tabInfo = new TabInfo("LOCAL_LIST", LocalProductList.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        
        mTabHost.setOnTabChangedListener(this);
	}
	
	/**
	 * Add Tab content to the Tabhost
	 * @param activity
	 * @param tabHost
	 * @param tabSpec
	 * @param clss
	 * @param args
	 */
	private static void AddTab(ProductTabsView activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
		// Attach a Tab view factory to the spec
		tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
	}

	/** (non-Javadoc)
	 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
	 */
	public void onTabChanged(String tag) {
		//TabInfo newTab = this.mapTabInfo.get(tag);
		int pos = this.mTabHost.getCurrentTab();
		
		if(pos != -1)
		this.mViewPager.setCurrentItem(pos);
    }

	/* (non-Javadoc)
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
	 */
	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
	 */
	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		this.mTabHost.setCurrentTab(position);
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
	 */
	@Override
	public void onPageScrollStateChanged(int state) {
		// TODO Auto-generated method stub
		
	}
}

